import re
import requests
import json
import random
with open('videos.json','r',encoding='utf8') as vj:
    # videosJson = json.load(videosJson)
    videoJson = json.load(vj)

with open('gifs.json', 'r', encoding='utf8') as gj:
    gifJson = json.load(gj)

with open('showimg.json', 'r', encoding='utf8') as sj:
    showJson = json.load(sj)

with open('gifs_22.json', 'r', encoding='utf8') as gj41:
    gifJson41 = json.load(gj41)

def getDownLoadUrls():
    i = 134000
    data = videoJson["videos"]
    # data = []
    while(i > 133000):
        rand = random.random()
        get_url = f"https://www.yugaopian.cn/?view=api&mode=download-data&rand={rand}&ttid={i}"
        # print(get_url)
        req_url = getDownLoadUrl(get_url)
        if req_url != None:
            print(i, req_url.group())
            data.append({
                'type': "mp4",
                'url': req_url.group().replace('\\', '')
            })
        i = i - 1
    return data




def getDownLoadUrl(url):
    html = requests.get(url)
    # print(html, html.text)
    html.encoding = "utf-8"
    return re.search("https(.*?)\.mp4", html.text)



def setJson(data):
    # print(data)
    with open('videos.json', 'w') as vj:
        json.dump(data, vj)



def saveVideoUrls():
    data = getDownLoadUrls()
    setJson({
        'videos': data
    })


def main():
    saveVideoUrls()



# https://www.soogif.com/shareSatin
def saveGifs(data):
    for i in range(0, 200):
        data.extend(formartData(getGifs(i * 20)))
    with open('gifs_22.json', 'w') as gj22:
        json.dump(data, gj22)
#
# labelId 13萌宠  21游戏  16萌娃  35沙雕  34尴尬  33科技艺术  32 装B  31牛人  29美女  28美食  27综艺  25作死  24整蛊  
# 23影视  22二次元  30内涵  37糗事  17脑回路  19斗图  26网红  38惊险  41体育#
# https://www.soogif.com/showimg?labelId=14&start=0&size=20
def getGifs(start):
    # html = requests.get(f"https://www.soogif.com/hotGif?start={start}&size=20")
    html = requests.get(f"https://www.soogif.com/showimg?labelId=22&start={start}&size=20")
    html.encoding = "utf-8"
    return re.findall("(http.*?\.gif)", html.text)
     

def formartData(url_list):
    data = []
    for url in url_list:
        data.append({
            'type': 'gif',
            'url': url
        })
    print(data)
    return data


# https://www.ssyer.com/detail/2790954
# https:\u002F\u002Fimg.ssyer.com\u002Fpicture\u002Fnew\u002Fzl\u002F00145b1ce55ab1db5152d108662f69c1.mp4
# https://img.ssyer.com/picture/new/zl/00145b1ce55ab1db5152d108662f69c1.mp4
def getVideosFromSsyer(id):
    html = requests.get(f"https://www.ssyer.com/detail/{id}")
    html.encoding = "utf-8"
    return re.search("https(.*?)\.mp4", html.text)


def saveVideosFromSsyer():
    data = []
    for i in range(2790000, 2790955):
        url = getVideosFromSsyer(i)
        if url != None:
            data.append({
                'type': 'mp4',
                'url': url.group()
            })
        i = i - 1
        print(i, url)
    with open('videos_ssyer.json','w',encoding='utf8') as vsj:
        # vsJson = json.load(vsj).extend(data)
        json.dump(data, vsj)


if __name__ == '__main__':
    # main()
    # print(getGifs(0))
    saveGifs(showJson)
    # print(getVideosFromSsyer(2790954))
    # saveVideosFromSsyer()
    # print(getGifs(0))
    # test()
    # url = f"https://www.yugaopian.cn/?view=api&mode=download-data&rand={random.random()}&ttid={i}"
    # getDownLoadUrl(url)


# https://www.yugaopian.cn/?view=api&mode=download-data&rand=0.5708919237913663&ttid=134142
# https://www.yugaopian.cn/?view=api&mode=download-data&rand=0.15598230540950153&ttid=134991
# https://www.yugaopian.cn/?view=api&mode=download-data&rand=0.08054564930634278&ttid=134992
# /?view=api&mode=download-data&rand=" + Math.random(), 